package com.tiger.log.core.config;

import com.tiger.log.common.constant.ThreadProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ThreadPoolConfiguration
 * @Description TODO
 * @Author huxuehao
 **/
@Configuration
public class ThreadPoolConfiguration {

    @Bean(name = "logCustomThreadPoolExecutor")
    public ThreadPoolExecutor threadPoolExecutor(ThreadProperties threadProperties) {
        return new ThreadPoolExecutor(
                threadProperties.getCoreSize(),
                threadProperties.getMaxSize(),
                threadProperties.getAliveTime(),
                TimeUnit.SECONDS,
                //默认是Integer的最大值，内存肯定会不够。所以需要指定一个数量
                new LinkedBlockingQueue<>(threadProperties.getQueueCapacity()),
                Executors.defaultThreadFactory(),
                //新进来的任务，直接丢弃
                new ThreadPoolExecutor.AbortPolicy()
        );
    }
}
